﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is used to exchange rgb channels of a pic.
    /// If you e.g. want to exchange red with green and green with blue:
    /// and red with blue:
    /// use 
    ///     "gbr"
    /// instead of 
    ///     "rgb"
    /// 
    /// <see cref="LambdaCommand"/>You might also use a lambda command to do this</see>
    /// </summary>
    public class RGBExchangeChannels : BaseImageFilter
    {
        protected string m_sRGB = string.Empty;

        /// <summary>
        /// Default constructor
        /// </summary>
        public RGBExchangeChannels()
            : this("bgr")
        {
        }

        /// <summary>
        /// Primary constructor to specify rgb string
        /// </summary>
        /// <param name="_sRGB"></param>
        public RGBExchangeChannels(string _sRGB)
        {
            m_sRGB = _sRGB;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_exchangeChannels"></param>
        internal RGBExchangeChannels(RGBExchangeChannels _exchangeChannels)
        {
            this.m_sRGB = _exchangeChannels.m_sRGB;
        }

        /// <summary>
        /// Gets or sets RGB
        /// </summary>
        public string RGB
        {
            get { return m_sRGB; }
            set { m_sRGB = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            Color color = Color.Transparent;

            int x = 0;
            int y = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    color = ColorFunctions.ExchangeChannels(color, m_sRGB);
                    bitmap.SetPixel(x, y, color);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Creates a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new RGBExchangeChannels(this);
        }
    }
}
